package textfield;

/**
 * Action.java
 *
 *
 * Created: Sat Jul 24 10:34:56 2010
 *
 * @author <a href="mailto:fred@theseibels.com">Fred Seibel</a>
 * @version 1.0
 */
public class Action implements Operation{
    private String tag;
    private String narrative;
    private String next = null;
    public Action(String tag, String narrative, String next) {
	Proto.operations.put(tag, this);
	this.tag = tag;
	this.narrative = narrative;
	this.next = next;
    } // Action constructor

    public Operation doOperation(Device device) {
	Operation op =  (Operation) Proto.operations.get(next);
	String text = getText();
	while (op instanceof Action) {
	    Action nextOp = (Action) op;
	    text += "\n\n" + nextOp.getText();
	    op =  (Operation) Proto.operations.get(nextOp.next);
	}
	// write text, and done flag
	boolean done = op == null;
	device.writeNarrative(text, done);
	return op;
    }
    
    String getText() {
	try {
	    return Proto.reconstruct.getLine(Integer.valueOf(narrative).intValue());
	}
	catch (java.lang.NumberFormatException e) {
	    return narrative;
	}
    }
    public String toString() {
	String retval = "Action " + tag + " " + narrative + " " + next + "\n";
	return retval;
    }
} // Action
